#! /usr/bin/env/python3
# coding:UTF-8
'''
    名称：掷骰子
    作者：王楠
    版本: 3.0
    日期：2020/03/01
    功能：dice_num，几个骰子
         3.0: 数据可视化 散点图
'''
import random
import matplotlib.pyplot as plt


class Dice():
    def roll_dice(self):
        numeral = random.randint(1, 6)
        return numeral


def dice_main():
    '''自己写：
    nums = []
    roll_times = int(input('请输入投掷次数：'))
    dice_num = int(input('请输入骰子个数：'))
    result_list_times = [0] * (6 * dice_num - dice_num + 1)
    # result_list_numeral = list(range(dice_num, 6 * dice_num + 1))
    # result_dict = dict(zip(result_list_numeral, result_list_times))
    dice = Dice(dice_num)
    for i in range(roll_times):
        num = dice.roll_dice()
        nums.append(num)
        for x in range(dice_num, 6 * dice_num + 1):
            if num == x:
                result_list[x-dice_num] += 1
    print(nums)
    print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    for i, result in enumerate(result_list):
        print('{}出现了{}次,频率是{}%'.format(
            i + dice_num, result,
            result / roll_times * 100))
    '''
    result_sum_list = []
    dice_case_list = []
    roll_times = int(input('请输入投掷次数：'))
    dice_num = int(input('请输入骰子个数：'))
    numeral_list = [[] for i in range(dice_num)]
    print(numeral_list)
    result_list_times = [0] * (6 * dice_num - dice_num + 1)
    result_list_numeral = list(range(dice_num, 6 * dice_num + 1))
    result_dict = dict(zip(result_list_numeral, result_list_times))
    for i in range(dice_num):
        dice_case_list.append(Dice())
    for i in range(roll_times):
        sum_of_dices = 0
        for n in range(dice_num):
            numeral_list[n].append(dice_case_list[n].roll_dice())
            sum_of_dices += numeral_list[n][i]
        for x in result_list_numeral:
            if sum_of_dices == x:
                result_dict[x] += 1
                result_sum_list.append(x)
    print(numeral_list)
    print(result_sum_list)
    print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    # for result_num in result_list_numeral:
    #     print('出现点数{0}的次数是{1}次,频率是{2:.1f}%'.format(
    #         result_num, result_dict[result_num],
    #         result_dict[result_num] / roll_times * 100))
    # print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    for result_num, result_times in result_dict.items():
        print('出现点数{0}的次数是{1}次,频率是{2:.1f}%'.format(
            result_num, result_times,
            result_times / roll_times * 100))
    # 数据可视化
    x = list(range(1, roll_times + 1))
    y = result_sum_list
    plt.scatter(x, y)
    plt.show()


if __name__ == '__main__':
    dice_main()
